/************************************************************************
 * File: game.h                                                         *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#ifndef GAME_H
#define GAME_H

#include <QWidget>
#include <QList>
#include "tile.h"
#include "globals.h"

class QString;
class Board;
class Company;
class Player;
class Bag;
class QTextEdit;
class QTextDocument;
class Button;
class Label;

class Game : public QWidget
{
	Q_OBJECT
	
	public:
		enum Validity {Valid, Invalid, Temporary};
		
		Game(QWidget *parent = 0);
		
		void advanceTurnCounter(Player *&turn) const;
		
		void setBoardSize(int rows, int columns);
		void addCompany(Company *company);
		void addPlayer(Player *player);
		void clearPlayers();
		void setupGUI();
		void setStartingCash(int value);
		void setInitialShares(int value);
		void setTilesInHand(int value);
		void setSafeSize(int value);
		void setBuyLimit(int value);
		void setMonopolySize(int value);
		void reset();
		void placeTileFromBag();
		void nextTurn();
		Validity isValidTile(const Tile &tile);
		void changePhase(Profit::Phase phase);
		void setupMergePhase();
		void formNewCompany(Company *company);
		void growCompany();
		void mergeCompanies(Company *big, Company *small);
		void clearCompanyMergeFlags();
		void nextMergeTurn();
		void updateMajorities(Company *company);
		void payBenefits(Company *company);
		void buyShare(Player *player, Company *company);
		void sellShare(Player *player, Company *company);
		void tradeShare(Player *player, Company *big, Company *small);
		void giveShare(Player *player, Company *company);
		void updateCompanies();
		void clearHelp();
		void setHelp(const QString &left, const QString &right = QString());
		void updateCompaniesInMerge();
		
	signals:
		void phaseChanged(Profit::Phase newPhase);
		void newTurn(Player *player);
		void gameEnded();
		void message(const QString &msg);
		
	protected:
		void paintEvent(QPaintEvent *event);
		
	private slots:
		void boardClicked(int row, int column);
		void companyClicked(Company *company);
		void doneClicked();
		void endClicked();
		void companySizeChanged(int size);
		void companyHoverChanged(bool hover, Company *company);
		void boardHoverChanged(bool hover, int row, int column);
		void doneButtonHoverChanged(bool hover);
		void endButtonHoverChanged(bool hover);
		
	private:
		void aiPhase();
		void aiSetDesirability(Player *ai);
		Company *aiChooseNewCompany(Player *ai);
		Company *aiChooseMerge(Player *ai);
		void aiMergeCompany(Player *ai, Company *big, Company *small);
		void aiBuyStock(Player *ai);
		Tile aiChooseTile(Player *ai);
	
		Board *m_board;
		Bag *m_bag;
		QList<Company*> m_companies;
		QList<Player*> m_players;
		Player *m_turn;
		Profit::Phase m_phase;
		QList<Tile> m_phaseTiles;
		QList<Company*> m_phaseCompanies;
		Player *m_mergeTurn;
		Button *m_doneButton;
		Button *m_endButton;
		Label *m_instruction;
		Label *m_leftHelp;
		Label *m_rightHelp;
		
		int m_startingCash;
		int m_initialShares;
		int m_tilesInHand;
		int m_safeSize;
		int m_buyLimit;
		int m_monopolySize;
};

#endif